﻿using System.Linq;
using System.Configuration;
using ServiceStack.Redis;

namespace Utility
{
    public static class RedisContext
    {
        //private static RedisSentinel _redisSentinel = null;
        //private static IRedisClientsManager _redisManager = null;
        private static PooledRedisClientManager prcm;

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init(string redisConnString)
        {
            //string[] serverList = SplitString(redisConnString, ",");
            //_redisSentinel = new RedisSentinel(serverList, null);
            //_redisManager = _redisSentinel.Start();
            Init(redisConnString, null);
        }
        /// <summary>
        /// 初始化重载
        /// </summary>
        /// <param name="redisConnString"></param>
        /// <param name="master"></param>
        public static void Init(string redisConnString, string master)
        {
            string[] serverList = SplitString(redisConnString, ",");
            //_redisSentinel = new RedisSentinel(serverList, master);
            // _redisManager = _redisSentinel.Start();
            prcm = new PooledRedisClientManager(serverList, serverList,
                              new RedisClientManagerConfig
                              {
                                  MaxWritePoolSize = 600,
                                  MaxReadPoolSize = 600,
                                  AutoStart = true,
                              });
        }

        private static string[] SplitString(string strSource, string split)
        {
            return strSource.Split(split.ToArray());
        }

        /// <summary>
        /// 客户端缓存操作对象
        /// </summary>
        public static IRedisClient GetClient()
        {
            return prcm.GetClient();
        }

        /// <summary>
        /// 客户端缓存操作对象
        /// </summary>
        public static IRedisClient GetReadOnlyClient()
        {
            return prcm.GetClient();
        }
        public static void RelaseClient()
        {
            prcm.Dispose();
        }

    }
}
